﻿
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MenuGame2DXNA
{
    public class UserControlledSprite : Sprite
    {
        private bool isCharacter;
        /// <summary>
        /// Initializes a new instance of the <see cref="UserControlledSprite"/> class.
        /// </summary>
        /// <param name="_inputTexture">The _input texture.</param>
        /// <param name="_position">The _position.</param>
        public UserControlledSprite(Texture2D[] _inputTexture, Vector2 _position) : base(_inputTexture, _position)
        {
            Stop = true;
            isCharacter = false;
        }

        /// <summary>
        /// Gets or sets the frame start.
        /// </summary>
        /// <value>
        /// The frame start.
        /// </value>
        public int FrameStart
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the frame end.
        /// </summary>
        /// <value>
        /// The frame end.
        /// </value>
        public int FrameEnd
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="UserControlledSprite"/> is stop.
        /// </summary>
        /// <value>
        ///   <c>true</c> if stop; otherwise, <c>false</c>.
        /// </value>
        private bool _stop;
       
        public bool Stop
        {
            get
            {
                return _stop;
                
            }
            set
            {
                
               
                    _stop = value;
                    if (_stop && isCharacter)
                        currentFrame = 4;
            }
        }

        private int times = 0;
        /// <summary>
        /// Đặt khoảng khoảng frame mà sprite lặp lại
        /// </summary>
        /// <param name="frameStart">The frame start.</param>
        /// <param name="frameEnd">The frame end.</param>
        public void SetFrame(short frameStart, short frameEnd, bool _isCharacter =true)
        {
            FrameStart = frameStart;
            FrameEnd = frameEnd;
            
            Stop = false;
            isCharacter = _isCharacter;
        }
        
        public override void Update(GameTime gameTime)
        {
            if (times % (10 - Speeds) == 0)
            {
                if (!Stop)
                    currentFrame = (currentFrame + 1) % (FrameEnd - FrameStart + 1);
            }
            times++;
           

        }
        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            
                
            Rectangle rect;
            if (Size.X == 0)
            {
                rect = new Rectangle((int)Position.X, (int)Position.Y, Textture2d[currentFrame + FrameStart].Width, Textture2d[currentFrame+FrameStart].Height);
            }
            else
            {
                rect = new Rectangle((int)Position.X, (int)Position.Y, (int)Size.X, (int)Size.Y);
            }
            spriteBatch.Draw(Textture2d[currentFrame + FrameStart], rect, Color);
        }
    }
}
